//
//  TBRRegisterController.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/4.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRRegisterController.h"
#import <JKCountDownButton.h>
#import "TBRMineSettingC.h"
#import "NetWorkRequest.h"
#import "NSString+MD5Addition.h"
@interface TBRRegisterController ()
@property (weak, nonatomic) IBOutlet UIView *view_bg;
@property (weak, nonatomic) IBOutlet UITextField *tf_phone;
@property (weak, nonatomic) IBOutlet UITextField *tf_yanZhengCode;

@property (weak, nonatomic) IBOutlet UITextField *tf_password;
@property (weak, nonatomic) IBOutlet UITextField *tf_rePassword;
@property (weak, nonatomic) IBOutlet UIButton *btn_login;
@property (weak, nonatomic) IBOutlet JKCountDownButton *btn_getYanZhengMa;

@end

@implementation TBRRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.view_bg.layer.borderColor = [UIColor colorWithHex:0xbf0021].CGColor;
    self.view_bg.layer.borderWidth = 1;
    self.view_bg.layer.cornerRadius = 5;
    self.view_bg.layer.masksToBounds = YES;
    _btn_login.layer.cornerRadius = 5;
    _btn_login.layer.masksToBounds = YES;
}


- (IBAction)closeBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];

}


- (IBAction)checkCodeBtnClick:(JKCountDownButton *)sender {
    [self.view endEditing:YES];
    //button type要 设置成custom 否则会闪动
    
    // MARK: - 获取验证码0-------
    [self getYanZhengMaBtnClick];
}

- (IBAction)registerBtnClick:(UIButton *)sender {
//    [self progressHUDShowWithCompleteText:@"注册成功" isSucceed:YES];
//    TBRMineSettingC *mineSettingC = [[TBRMineSettingC alloc] init];
//    [self.navigationController pushViewController:mineSettingC animated:YES];
//    mineSettingC.isShowSkipBtn = YES;
    //跳转到填写个人信息界面--------------------------
    NSString *account = self.tf_phone.text;
    NSString *passwordStr = self.tf_password.text;
    NSString *codeStr = self.tf_yanZhengCode.text;
    NSString *rePasswordStr = self.tf_rePassword.text;
    if (![passwordStr isEqualToString:rePasswordStr]) {
        self.noticeText = @"两次密码输入不一致";
        return;
    }
    NSDictionary *parDic = @{@"phone":account,@"password":[passwordStr MD5String],@"verifycode":codeStr,@"devicetoken":@"ios",@"mode":@"2"};
    [self loginBtnDidClick:parDic];
}


// MARK: - 获取验证码网络事件-------
- (void)getYanZhengMaBtnClick {
    // MARK: - 登录操作--------------
    NSString *phone = self.tf_phone.text;
    NSDictionary *parDic = @{@"phone":phone};
    if ([HelpObject isBlankString:phone]) {
        self.noticeText = @"请先输入手机号码";
        return;
    }else if (phone.length != 11) {
        self.noticeText = @"请输入11位手机号码";
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"获取验证码..."];
    [NetWorkRequest postNetRequestData:getYanZhengMa parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            weakSelf.btn_getYanZhengMa.enabled = NO;
            [weakSelf.btn_getYanZhengMa startCountDownWithSecond:60.0];
            [weakSelf.btn_getYanZhengMa countDownChanging:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                NSString *title = [NSString stringWithFormat:@"剩余%ld秒",second];
                return title;
            }];
            [weakSelf.btn_getYanZhengMa countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                countDownButton.enabled = YES;
                return @"获取验证码";
            }];
            [weakSelf progressHUDShowWithCompleteText:@"验证码获取成功" isSucceed:YES];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"验证码获取失败" isSucceed:NO];
    }];
}


// MARK: - 注册并登录网络事件-------
- (void)loginBtnDidClick:(NSDictionary *)dic {
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"登录中..."];
    // MARK: - 登录操作--------------
    [NetWorkRequest postNetRequestData:RegisterTD parmeters:dic response:^(NSDictionary *response) {
        if (response) {
            //保存数据---进行一系列的操作----------
            [weakSelf progressHUDShowWithCompleteText:@"登录成功" isSucceed:YES];
            [AppSetting shareAppSetting].isLogin = @"1";
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"登录失败，请重新登录" isSucceed:NO];
    }];
}

@end
